package com.product.android.ui.dialog;


import java.util.regex.Pattern;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.common.android.utils.ToastUtils;
import com.nd.android.u.allcommon.R;

public class DlgModifyText extends Dialog {
	
	private static final int DISCUSSION_NAME_MIN_LENGTH = 2;
	private static final int DISCUSSION_NAME_MAX_LENGTH = 18;
	
	public static final int TEXT_TYPE_PHONE=0;
	public static final int TEXT_TYPE_EMAIL=1;
	public static final int TEXT_TYPE_DISCUSSION=2;
	private String m_strDlgTitle;
	private String m_strHint;
	private String m_strDefaultValue;
	
	private TextView m_tvTitle;
	private EditText m_etTextValue;
	
	private Button 		m_btnCancel;
	private Button 		m_btnSave;
	private Button 		m_btnReset;
	private Context m_context;
	
	//文本类型	0:手机，1:邮箱，2：讨论组
	private int		m_iTextType = -1;
	
	private OnDlgModifyTextDismissListener m_DismissListener;
	
	public interface OnDlgModifyTextDismissListener{
		abstract void GetTextValue(String strTextValue);
	}
	
	public void setOnAfterDismissListener(OnDlgModifyTextDismissListener dismissListener){
		this.m_DismissListener = dismissListener;
	}
	
	public DlgModifyText(Context context, int iTextType, String strDlgTitle, String strHint, String strDefaultVaule, int Resid) {
		super(context, Resid);
		m_context = context;
		m_iTextType = iTextType;
		m_strDlgTitle = strDlgTitle;
		m_strHint = strHint;
		m_strDefaultValue = strDefaultVaule;
	}
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.dlg_modify_text);	
		
		m_btnCancel = (Button)findViewById(R.id.button_cancel);
		m_btnSave = (Button)findViewById(R.id.button_ok);
		
		//del by linmh at 20140905 Fix Bug #30189
		//m_btnReset = (Button)findViewById(R.id.button_reset);
		m_btnCancel.setOnClickListener(clickListener);	
		m_btnSave.setOnClickListener(clickListener);
		
		//del by linmh at 20140905 Fix Bug #30189
		//m_btnReset.setOnClickListener(clickListener);
		
		m_tvTitle = (TextView)findViewById(R.id.dlg_title);
		m_tvTitle.setText(m_strDlgTitle);
		
		m_etTextValue = (EditText)findViewById(R.id.text_value);
		m_etTextValue.setHint(m_context.getString(R.string.personal_input, m_strHint));
		
		if(TEXT_TYPE_PHONE == m_iTextType){
			m_etTextValue.setInputType(InputType.TYPE_CLASS_NUMBER);
			//再设置长度？
		}
		
		if(!TextUtils.isEmpty(m_strDefaultValue)){
			m_etTextValue.setText(m_strDefaultValue);
			m_etTextValue.setSelection(m_strDefaultValue.length());
		}
		
		if (TEXT_TYPE_DISCUSSION == m_iTextType) {
			//mod by linmh at 20140905 Fix Bug# 30189
			/*String hint = m_context.getString(
					R.string.discussion_name_limit_hint,
					DISCUSSION_NAME_MAX_LENGTH);
			m_etTextValue.setHint(hint);*/
			m_etTextValue.setHint(R.string.group_name_default);
			//end of mod
			
			InputFilter[] filters = { new LengthFilter(
					DISCUSSION_NAME_MAX_LENGTH) };
			m_etTextValue.setFilters(filters);
		}
	}	
	
	protected Button.OnClickListener clickListener = new Button.OnClickListener() {
		@Override
		public void onClick(View v) {
			int id=v.getId();
			if(id == R.id.button_cancel){
				dismiss();
			}
			else if(id == R.id.button_ok){
				String input=m_etTextValue.getText().toString().trim();
				if (TextUtils.isEmpty(input)){
					//为空提示输入
					Toast.makeText(m_context,m_context.getString(R.string.personal_save, m_strHint), Toast.LENGTH_LONG).show();
					return;
				}
				if (m_iTextType!=TEXT_TYPE_DISCUSSION){
					//不是讨论组
					String str="";
					String errText=m_context.getString(R.string.personal_input, m_strHint);
					if (m_iTextType==TEXT_TYPE_EMAIL){
						str="^[0-9a-z._-]+@(([0-9a-z]+)[.])+[a-z]{2,3}$";
					}else{
						str="1\\d{10}";
					}
					//判断输入的格式
			        Pattern pattern = Pattern.compile(str);
			        if (!pattern.matcher(input).matches()){
			        	Toast.makeText(m_context, errText, Toast.LENGTH_LONG).show();
			        	return;
			        }
				}else{
					if (input.length()>DISCUSSION_NAME_MAX_LENGTH){
						ToastUtils.display(m_context, R.string.discussion_name_limit_max);
						return;
					}
					else if(input.length()<DISCUSSION_NAME_MIN_LENGTH){
						ToastUtils.display(m_context,R.string.discussion_name_limit_min);
						return;
					}
				}
				if(null != m_DismissListener){
					getValueAndDismiss();
				}
			}
			/* del by linmh at 20140905 Fix Bug #30189
			 * else if(id == R.id.button_reset){
				m_etTextValue.setText("");			
			}*/
		}
	};
	
	private void getValueAndDismiss(){
		m_DismissListener.GetTextValue(m_etTextValue.getText().toString().trim());
		dismiss();
	}
}
